package com.huawei.hms.framework.network.grs;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.dnkeeper.DNKeeperConfig;
import com.huawei.hms.framework.network.restclient.hwhttp.plugin.BasePlugin;
import com.huawei.hms.framework.network.restclient.hwhttp.plugin.PluginInterceptor;
import java.io.IOException;

public class GrsManager implements BasePlugin {
    private static final int GRS_KEY_INDEX = 1;
    private static final int GRS_PATH_INDEX = 2;
    private static final String GRS_SCHEMA = "grs://";
    private static final int GRS_SERVICE_INDEX = 0;
    private static final int MAX_GRS_SPLIT = 3;
    private static final String SEPARATOR = "/";
    private static final String TAG = "GrsManager";
    @SuppressLint({"StaticFieldLeak"})
    private static volatile GrsManager instance;
    private GrsConfig grsConfig;
    private c grsInterceptor;

    private GrsManager() {
    }

    public static GrsManager getInstance() {
        if (instance == null) {
            synchronized (GrsManager.class) {
                if (instance == null) {
                    instance = new GrsManager();
                }
            }
        }
        return instance;
    }

    private String getServiceNameUrl(String str, String str2) {
        return GrsApi.synGetGrsUrl(str, str2);
    }

    public static boolean isGRSSchema(String str) {
        return str != null && str.startsWith(GRS_SCHEMA);
    }

    private String[] parseGRSSchema(String str) {
        return str.substring(str.toLowerCase().indexOf(GRS_SCHEMA) + GRS_SCHEMA.length()).split(SEPARATOR, 3);
    }

    public PluginInterceptor getInterceptor() {
        if (this.grsInterceptor == null) {
            this.grsInterceptor = new c();
        }
        return this.grsInterceptor;
    }

    public boolean initGrs(Context context, GrsConfig grsConfig) {
        if (this.grsConfig == null || !this.grsConfig.equal(grsConfig)) {
            this.grsConfig = grsConfig;
            if (context != null) {
                context = context.getApplicationContext();
            }
            GrsApi.grsSdkInit(context, this.grsConfig != null ? this.grsConfig.getGrsBaseInfo(context) : null);
        }
        return true;
    }

    protected String parseGrs(String str) {
        if (str.endsWith(SEPARATOR)) {
            str = str.substring(str.indexOf(GRS_SCHEMA), str.length() - 1);
        }
        String[] parseGRSSchema = parseGRSSchema(str);
        String str2 = "";
        if (parseGRSSchema.length == 1) {
            str2 = getServiceNameUrl(parseGRSSchema[0], DNKeeperConfig.SERVICE_KEY);
        } else if (parseGRSSchema.length >= 2) {
            str2 = getServiceNameUrl(parseGRSSchema[0], parseGRSSchema[1]);
        } else {
            Logger.i(TAG, "parseGrs params.length<1.");
        }
        if (!TextUtils.isEmpty(str2)) {
            return parseGRSSchema.length > 2 ? str2.endsWith(SEPARATOR) ? str2 + parseGRSSchema[2] : str2 + SEPARATOR + parseGRSSchema[2] : str2;
        } else {
            throw new IOException("can not get url, do grsUrl(serviceName or key) error?");
        }
    }
}
